
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

#include "RTTypes.h"

#include <list>
using std::list;

#include "RTBase.h"
#include "RTSource.h"

//include parameter here to avoid to have to put it in every specific controller
#include "RTParameter.h"

namespace RT
{
	/*Base class controller.												*/
	/*Inherit from this class to create a specific controller.				*/
	template <typename T> class RTController : public RTSource<T>
	{
	private:
	
		/* the output parameter of the controller */
		RTParameter<T>		_output;	

		/* a liste of parameter used to compute the value of the controller */
		list<RTBase*>		_dependent; 

		/* indicate if the value of the output parameter has to be calculated*/
		bool				_dirty;		

	public:
		/*Constructor*/
		RTController();

		/*Destructor*/
		virtual ~RTController();

		/*Register a parameter to the controller*/
		void registerDependent(RTBase*);

		/*calculate and return the value of the output parameter*/
		const T& getValue();

		/*set the value of the output parameter*/
		void setValue(T);

		/*return a poI32er to the output parameter of the controller*/
		RTParameter<T>* getOutputRTParameter();

		/*calculate the value of the output parameter. This function should never be called outside of this class.*/
		void onDemand();

		/*dirty the controller. should never be used outside of this class.*/
		void onChange();

	private:
		/*update (calculate) the controller. This function should be overwritten to create a specific controller.*/ 
		virtual void doUpdate() = 0;
	};


	//constructor
	template <typename T> RTController<T>::RTController():_dirty(true)
	{
		_output.init(this, 0, true);
		registerDependent(&_output);
	}

	//destructor
	template <typename T> RTController<T>::~RTController()
	{
		_dependent.clear();
	}

	//register a parameter
	template <typename T> void RTController<T>::registerDependent(RTBase* p)
	{
		_dependent.push_front(p);
	}

	//calculate and return the value of the output parameter
	template <typename T> const T& RTController<T>::getValue()
	{
		return _output.getValue();
	}

	//set the value of the output parameter
	template <typename T> void RTController<T>::setValue(T value)
	{
		_output.setValue(value);
	}

	//return a poI32er to the output parameter
	template <typename T> RTParameter<T>* RTController<T>::getOutputRTParameter()
	{
		return &_output;
	}

	//update the controller
	template <typename T> void RTController<T>::onDemand()
	{
		if (_dirty)
		{
			doUpdate();
			_dirty = false;
		}
	}

	//dirty the controller
	template <typename T> void RTController<T>::onChange()
	{
		if (_dirty)
			return;		

		_dirty = true;
		for ( list<RTBase*>::iterator it = _dependent.begin(); it != _dependent.end(); it++ )
			(*it)->onChange();
	}

}